package com.rookie.jeesdp.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Aspect
public class ContrellerAspect {

    private static Logger log = LoggerFactory.getLogger(ContrellerAspect.class);

    @Before("execution(public * com.rookie.jeesdp.controller.*.*(..))")
    public void deBefore(JoinPoint joinPoint){
        // 接收到请求，记录请求内容
        //ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        log.info("Class:" + joinPoint.getSignature().getDeclaringTypeName() + ",Method:" + joinPoint.getSignature().getName());
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            log.info("Param " + i + ":" + args[i].getClass().getSimpleName() + " " + args[i].toString());
        }
    }

    /*//后置异常通知
    @AfterThrowing("execution(public * com.rookie.jeesdp.controller.*.*(..))")
    public void doAfterThrowing(JoinPoint jp) {
        System.out.println("方法异常时执行.....");
    }*/

    /*//后置最终通知,final增强，不管是抛出异常或者正常退出都会执行
    @After("execution(public * com.rookie.jeesdp.controller.*.*(..))")
    public void doAfter(JoinPoint jp) {
        System.out.println("方法最后执行.....");
    }*/

    @AfterReturning(returning = "o", pointcut = "execution(public * com.rookie.jeesdp.controller.*.*(..))")
    public void doAfterReturning(Object o){
        // 处理完请求，返回内容
        log.info("Return:" + o.toString());
    }

    /*//环绕通知,环绕增强，相当于MethodInterceptor
    @Around("execution(public * com.rookie.jeesdp.controller.*.*(..))")
    public Object doAround(ProceedingJoinPoint pjp) {
        System.out.println("方法环绕start.....");
        try {
            Object o = pjp.proceed();
            System.out.println("方法环绕proceed，结果是 :" + o);
            return o;
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
    }*/
}
